<template>
  <div class="container">
    <scroll-view :scroll-y="true"
                 :style="{'height': '100%'}"
                 @scrolltolower="infiniteScroll">
      <div class="top clearfix"
           v-if="!isempty">
        <div class="left">
          <div class="select"
               v-if="show_radio">
            已选中
            <span class="pink">{{selected_totle}}</span> 条消息
          </div>
          <div class="num"
               v-else>
            共
            <span class="pink">{{totle}}</span> 条消息
          </div>
        </div>
        <div class="right"
             @click="status_change()">{{show_radio?'完成':'编辑'}}</div>
      </div>
      <ul class="list"
          v-if="!isempty">
        <li class="list-item"
            v-for="(item, index) in message_list"
            :key="index"
            @click.stop="godetail(item)">
          <div class="item-radio"
               @click.stop="change_status(item)"
               v-if="show_radio">
            <div class="circle"
                 :class="{'selected':item.selected}"></div>
          </div>
          <div class="ring">
            <img :src="base_url+'/shouye/xiaoxizhongxin@2x.png'"
                 v-if="base_url"
                 alt
                 class="img" />
          </div>
          <div class="msg">
            <div class="date">{{item.time}}</div>
            <div class="content">{{item.content}}</div>
          </div>
        </li>
      </ul>
      <div class="empty"
           v-if="isempty">
        <img src="https://img.ameimeika.com/h5_images/mp_images/mp_3.0/search/nonews.png"
             v-if="base_url"
             alt
             class="nothing" />
        <div class="text">什么消息都没有呢~</div>
      </div>
    </scroll-view>

    <div class="bottom clearfix"
         v-if="show_radio&&!isempty">
      <div class="left"
           @click="toggle_all()">
        <div class="circle"
             :class="{'selected':select_all}"></div>全选
      </div>
      <div class="right"
           @click="delete_item()">
        <img :src="base_url+'/shouye/shanchu-hongse@2x.png'"
             v-if="base_url"
             alt
             mode="widthFix"
             class="img" /> 删除
      </div>
    </div>
  </div>
</template>

<script>
import common from "@/assets/js/mmk_common.js";
import "@/assets/js/timeFormat.js";
export default {
  data () {
    return {
      base_url: common.image_resource,
      show_radio: false,
      message_list: [],
      selected_arr: [],
      page: 1,
      totle: 0,
      selected_totle: 0,
      select_all: false,
      isbottom: false,
      isempty: true
    };
  },
  methods: {
    /** */
    godetail (data) {
      //跳转对应页面
      console.log(data);
      if (data.type == 2) {
        //优惠卷列表
        wx.navigateTo({ url: `/pages/package_mine/ticket_list/main` });
      }
      if (data.type == 3 && data.url) {
        //H5
        wx.navigateTo({
          url: `/pages/package_main/advertisement/main?url=${encodeURIComponent(data.url)}`
        });
      }
      if (data.type == 4) {
        //商品关键词
        wx.navigateTo({
          url: `/pages/package_main/searchPage/main?type=2&words=${data.keyword}`
        });
      }
      if (data.type == 5) {
        //项目关键词
        wx.navigateTo({
          url: `/pages/package_main/search_index/main?status=2&keyword=${data.keyword}`
        });
      }

      if (data.type == 6) {
        //项目分类
        wx.navigateTo({
          url: `/pages/package_projects/three_level/main?cate_name=${data.project_title}&cate_id=${
            data.skip_id
            }`
        });
      }
      if (data.type == 7) {
        //商品分类
        wx.navigateTo({
          url: `/pages/package_projects/shop_type_detail/main?id=${data.skip_id}`
        });
      }
      if (data.type == 8) {
        //plus主页
        wx.switchTab({
          url: `/pages/plus/main`
        });
      }
      if (data.type == 9) {
        //生美主页
        wx.navigateTo({ url: "/pages/life_beauty/mian" });
        // wx.navigateTo({
        //   url: `/pages/shop_type_detail/main?id=${data.skip_id}`
        // });
      }
    },
    status_change () {
      if (this.show_radio) {
        let arr = [...this.message_list];
        arr.forEach(el => {
          el.selected = false;
        });
        this.message_list = arr;
        this.selected_totle = 0;
        this.selected_arr = [];
        this.select_all = false;
      }
      this.show_radio = !this.show_radio;
    },
    infiniteScroll () {
      this.page++;
      this.getlist();
    },
    getlist () {
      if (this.isbottom) {
        return false;
      }
      wx.showLoading({
        title: "加载中", //提示的内容,
        mask: true //显示透明蒙层，防止触摸穿透,
      });
      common.fly_post(
        "api/v4_5/user_messages/system_index",
        {
          page: this.page,
          pageSize: 20
        },
        result => {
          let res = result.data;
          wx.hideLoading();
          if (res.status_code == 0) {
            if (res.data.data.length == 0 && this.page > 1) {
              this.isempty = false;
              wx.showToast({
                title: "已经没有更多了",
                icon: "none",
                duration: 2000
              });
              this.isbottom = true;
              return false;
            } else if (res.data.data.length == 0 && this.page == 1) {
              this.isempty = true;
              wx.setStorageSync("msg_num", res.data.total);
              return false;
            } else if (res.data.data.length > 0 && this.page == 1) {
              wx.setStorageSync("msg_num", res.data.total);
              this.isempty = false;
              this.totle = res.data.total;
              this.message_list = res.data.data;
              this.message_list.forEach(element => {
                element.time = new Date(element.created_at * 1000).Format(
                  "yyyy-MM-dd hh:mm"
                );
              });
            } else {
              wx.setStorageSync("msg_num", res.data.total);
              this.isempty = false;
              this.totle = res.data.total;
              this.message_list.push(...res.data.data);
              this.message_list.forEach(element => {
                element.time = new Date(element.created_at * 1000).Format(
                  "yyyy-MM-dd hh:mm"
                );
              });
            }
          } else {
            wx.showToast({
              title: res.message,
              icon: "none",
              duration: 2000,
              mask: true
            });
          }
        }
      );
    },
    change_status (item) {
      if (this.show_radio) {
        // 编辑状态下点击
        let arr = [...this.message_list];
        arr.forEach(el => {
          if (el.id == item.id) {
            el.selected = !el.selected;
          }
          if (!el.selected) {
            this.select_all = false;
          }
        });
        this.message_list = arr;
        this.selected_arr = arr.filter(item => {
          return item.selected == true;
        });
        if (this.selected_arr.length == this.message_list.length) {
          this.select_all = true;
        }
        this.selected_totle = this.selected_arr.length;
      } else {
        // 非编辑状态下点击
      }
    },
    toggle_all () {
      this.select_all = !this.select_all;
      let arr = [...this.message_list];
      arr.forEach(el => {
        el.selected = this.select_all;
      });
      this.message_list = arr;
      this.selected_arr = arr.filter(item => {
        return item.selected == true;
      });
      this.selected_totle = this.selected_arr.length;
    },
    delete_item () {
      let that = this;
      if (this.selected_arr.length == 0) {
        wx.showToast({
          title: "请选择您要删除的消息",
          icon: "none",
          duration: 2000,
          mask: true
        });
        return false;
      } else {
        wx.showModal({
          title: "", //提示的标题,
          content: "确认删除？", //提示的内容,
          showCancel: true, //是否显示取消按钮,
          cancelText: "取消", //取消按钮的文字，默认为取消，最多 4 个字符,
          cancelColor: "#444444", //取消按钮的文字颜色,
          confirmText: "删除", //确定按钮的文字，默认为取消，最多 4 个字符,
          confirmColor: "#444444", //确定按钮的文字颜色,
          success: res => {
            if (res.confirm) {
              wx.showLoading({
                title: "删除中...", //提示的内容,
                mask: true //显示透明蒙层，防止触摸穿透
              });
              let arr = [];
              that.selected_arr.forEach(val => {
                arr.push(val.id);
              });
              common.fly_post(
                "api/v4_5/del_message",
                {
                  ids: JSON.stringify(arr)
                },
                result => {
                  let res = result.data;
                  wx.hideLoading();
                  if (res.status_code == 0) {
                    that.show_radio = false;
                    let arr = [...that.message_list];
                    Object.assign(this.$data, this.$options.data());
                    this.getlist();
                  }
                }
              );
            } else if (res.cancel) {
              return false;
            }
          }
        });
      }
    }
  },

  onLoad () {
    wx.setNavigationBarTitle({
      title: "消息中心"
    });
    Object.assign(this.$data, this.$options.data());
    this.getlist();
  },
  onShow () {
    //  this.getlist();
  }
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  background: rgba(251, 251, 251, 1);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .top {
    line-height: 40px;
    padding: 0 15px;
    .left {
      float: left;
      font-size: 14px;
      font-family: "Regular";
      color: rgba(136, 136, 136, 1);
      .pink {
        color: #f64665;
      }
    }
    .right {
      float: right;
      font-size: 14px;
      font-family: "Regular";
      color: rgba(68, 68, 68, 1);
    }
  }
  .list {
    width: 100%;
    flex: 1;
    padding-bottom: 50px;
    .list-item {
      width: 100%;
      min-height: 100px;
      background-color: #fff;
      display: flex;
      margin-bottom: 5px;
      .item-radio {
        width: 40px;
        position: relative;
        .circle {
          width: 20px;
          height: 20px;
          border-radius: 50%;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          border: 1px solid rgba(136, 136, 136, 1);
        }
        .selected {
          position: relative;
          &::before {
            content: "";
            display: block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: rgba(253, 142, 162, 1);
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
          }
        }
      }
      .ring {
        width: 60px;
        position: relative;
        .img {
          width: 50px;
          height: 50px;
          border-radius: 50%;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
      .msg {
        flex: 1;
        .date {
          font-size: 12px;
          font-family: "Regular";
          color: rgba(136, 136, 136, 1);
          line-height: 28px;
          margin-top: 16px;
        }
        .content {
          height: 40px;
          font-size: 14px;
          font-family: "Regular";
          color: rgba(68, 68, 68, 1);
          padding-right: 15px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }
      }
      .icon {
        width: 40px;
        position: relative;
        .img {
          width: 24px;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
    }
  }
  .empty {
    width: 154px;
    position: fixed;
    top: 100px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
    .nothing {
      width: 128px;
      height: 114px;
      margin: 0 auto;
      display: block;
    }
    .text {
      width: 100%;
      font-size: 14px;
      font-weight: 400;
      margin-top: 13px;
      color: rgba(153, 153, 153, 1);
      text-align: center;
    }
  }
  .bottom {
    width: 100%;
    height: 50px;
    line-height: 50px;
    padding: 0 15px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px -1px 0px 0px rgba(204, 204, 204, 0.5);
    position: fixed;
    bottom: 0;
    left: 0;
    box-sizing: border-box;
    .left {
      float: left;
      font-size: 14px;
      font-family: "Regular";
      color: rgba(68, 68, 68, 1);
      .circle {
        display: inline-block;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        border: 1px solid rgba(136, 136, 136, 1);
        margin-right: 5px;
        vertical-align: middle;
      }
      .selected {
        position: relative;
        &::before {
          content: "";
          display: block;
          width: 12px;
          height: 12px;
          border-radius: 50%;
          background: rgba(253, 142, 162, 1);
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
    }
    .right {
      float: right;
      font-size: 14px;
      font-family: "Regular";
      color: rgba(246, 70, 101, 1);
      img {
        width: 20px;
        margin-right: 5px;
        vertical-align: middle;
      }
    }
  }
}
</style>

